import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {IndexService} from "../services/index.service";

@Injectable()
export class IndexDataGuard implements CanActivate {
  constructor(private indexServices: IndexService) {
  }

  canActivate(next: ActivatedRouteSnapshot,
              state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if (!this.indexServices.getData().length) {
      return this.indexServices.requestData();
    } else {
      return true;
    }

  }
}
